package com.eop.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.eop.entity.EmergencyStage;
import com.eop.entity.Event;
import com.eop.entity.SpecialInfor;
import com.eop.service.IEmergencyStageService;
import com.eop.service.IEventService;
import com.eop.service.ISpecialInforService;

@ManagedBean(name = "eop_listEventBean", scope = ManagedBeanScope.REQUEST)
public class ListEventBean {
	private Log logger = LogFactory.getLog(ListEventBean.class);
	private IEventService eventService = SpringBeanHolder
			.getBean("eventService");
	private IEmergencyStageService imsService = SpringBeanHolder
			.getBean("stageService");
	private ISpecialInforService spInfoService = SpringBeanHolder
			.getBean("specialInforService");

	@Inject
	private FacesContext context;

	@Bind(id = "eventNameForSearch", attribute = "value")
	@SaveState
	private String eventNameForSearch;

	@Bind(id = "listEventGrid", attribute = "binding")
	private UIDataGrid eventGrid_binding;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			eventNameForSearch = null;
		}
	}

	@Action(id = "search")
	public void search() {
		eventGrid_binding.reload();
	}

	@Action(id = "reset")
	public void reset() {
		eventNameForSearch = null;
		eventGrid_binding.reload();
	}

	@DataModel(id = "listEventGrid")
	private List<Event> getEventGrid() {
		// 搜索条件写入搜索
		List<Event> chList = new ArrayList<Event>();
		if (eventNameForSearch != null && !"".equals(eventNameForSearch)) {
			chList = eventService.getEventByName(eventNameForSearch);
		} else {
			chList = eventService.getAllEffectEvent();
		}
		return chList;
	}

	@Action(id = "editEvent")
	public void editEvent() {
		Object[] obj = eventGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Event event = (Event) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/eop/editEvent.faces?id=" + event.getId();
		String script = "addCenterLayoutTab('editEventTab','编辑事件','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}

	@Action(id = "deleteEvent")
	public void deleteEvent() {
		Object[] obj = eventGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		Event event = (Event) obj[0];
		// 逻辑删除
		// schnarioService.deleteSchnario(sch);
		event.setEvent_DelFlag(true);
		eventService.saveEvent(event);
		// 循环删除 事件下面的所有阶段和应急事件
		List<SpecialInfor> siList = spInfoService.getSpecialInforByEvent(event
				.getId());
		for (int i = 0; i < siList.size(); i++) {
			siList.get(i).setDelFlag(true);
			spInfoService.saveSpecialInfor(siList.get(i));
		}
		List<EmergencyStage> esList = imsService.getEmergencyStageByEvent(event
				.getId());
		for (int i = 0; i < esList.size(); i++) {
			esList.get(i).setDelFlag(true);
			imsService.saveStage(esList.get(i));
		}
		eventGrid_binding.reload();
	}

}
